﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace DuanGiuaKy
{
    public partial class ctrlU_adm_qlSinhVien : UserControl
    {
        
        public ctrlU_adm_qlSinhVien()
        {
            InitializeComponent();
        }
        private void loadDataGridView()
        {
            dgvSinhVien.DataSource = SinhVien_BUS.loadTatCaSinhVien();
        }
        private void btnThemMoiSV_Click(object sender, EventArgs e)
        {
            frm_adm_qlSinhVien frm = new frm_adm_qlSinhVien();
            frm.ShowDialog();
            loadDataGridView();
        }

        private void ctrlU_adm_qlSinhVien_Load(object sender, EventArgs e)
        {
            loadDataGridView();
        }

        private void btnSuaThongTin_Click(object sender, EventArgs e)
        {
            try
            {
                int rInd = dgvSinhVien.CurrentCell.RowIndex;
                string masv = dgvSinhVien.Rows[rInd].Cells[0].Value.ToString();
                frm_adm_qlSinhVien frm = new frm_adm_qlSinhVien(2, masv);
                frm.ShowDialog();
                loadDataGridView();
            }
            catch (System.NullReferenceException)
            {
                if (dgvSinhVien.SelectedRows.Count == 0)
                    MessageBox.Show("Chọn sinh viên để chỉnh sửa!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnXoaSv_Click(object sender, EventArgs e)
        {
            try
            {
                int rInd = dgvSinhVien.CurrentCell.RowIndex;
                string masv = dgvSinhVien.Rows[rInd].Cells[0].Value.ToString();
                DialogResult res = MessageBox.Show("Bạn có chắc muốn xóa?", "Cảnh báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (res == DialogResult.OK)
                {
                    SinhVien_BUS.xoaThongTinSinhVien(masv);
                    loadDataGridView();
                }
            }
            catch
            {
                MessageBox.Show("Chọn sinh viên để xóa!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnXemThongTin_Click(object sender, EventArgs e)
        {
            try
            {
                int rInd = dgvSinhVien.CurrentCell.RowIndex;
                string masv = dgvSinhVien.Rows[rInd].Cells[0].Value.ToString();
                frm_adm_qlSinhVien frm = new frm_adm_qlSinhVien(3, masv);
                frm.ShowDialog();
            }
            catch
            {
                MessageBox.Show("Chọn sinh viên để xem thông tin chi tiết!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

    }
}
